﻿using System.Collections.Generic;
using InspectionApi.Enum;
using Microsoft.EntityFrameworkCore;
using netCoreStudy.Entities;
using static netCoreStudy.MyCommonMethod;

namespace netCoreStudy
{
	public class ApiDbContext : DbContext
	{
		public ApiDbContext(DbContextOptions<ApiDbContext> options) : base(
			options)
		{
		}

		public DbSet<User> Users { get; set; }
		public DbSet<Role> Roles { get; set; }
		public DbSet<Menu> Menus { get; set; }
		public DbSet<Material> Materials { get; set; }

		protected override void OnModelCreating(ModelBuilder modelBuilder)
		{
			// 配置多对多 设置 多对多表的 联合主键
			modelBuilder.Entity<UserRole>().HasKey(x => new {x.RoleId, x.UserId});
			modelBuilder.Entity<RoleMenu>().HasKey(x => new {x.RoleId, x.MenuId});

			SetBaseData(modelBuilder);

			SetExtendData(modelBuilder);
		}

		/// <summary>
		/// 设置扩展数据
		/// </summary>
		/// <param name="modelBuilder"></param>
		private void SetExtendData(ModelBuilder modelBuilder)
		{
		}

		/// <summary>
		/// 设置基础数据 
		/// </summary>
		private void SetBaseData(ModelBuilder modelBuilder)
		{
			modelBuilder.Entity<User>().HasData(
				new User {Id = 1, Name = "ifnk", Password = Md5Encrypt32("ifnk"), MaterialId = 1},
				new User {Id = 2, Name = "test", Password = Md5Encrypt32("test"), MaterialId = 1},
				new User {Id = 3, Name = "老李", Password = Md5Encrypt32("ifnk"), MaterialId = 1},
				new User {Id = 4, Name = "admins", Password = Md5Encrypt32("ifnk"), MaterialId = 1},
				new User {Id = 5, Name = "xx", Password = Md5Encrypt32("ifnk"), MaterialId = 1},
				new User {Id = 6, Name = "xx1", Password = Md5Encrypt32("ifnk"), MaterialId = 1},
				new User {Id = 7, Name = "tiBug", Password = Md5Encrypt32("ifnk"), MaterialId = 1},
				new User {Id = 8, Name = "test", Password = Md5Encrypt32("ifnk"), MaterialId = 1},
				new User {Id = 9, Name = "xx2", Password = Md5Encrypt32("ifnk"), MaterialId = 1},
				new User {Id = 10, Name = "xx3", Password = Md5Encrypt32("ifnk"), MaterialId = 1},
				new User {Id = 11, Name = "xx4", Password = Md5Encrypt32("ifnk"), MaterialId = 1},
				new User {Id = 12, Name = "blogadmin", Password = Md5Encrypt32("blogadmin"), MaterialId = 1},
				new User {Id = 13, Name = "test2", Password = Md5Encrypt32("ifnk"), MaterialId = 1},
				new User {Id = 14, Name = "xx5", Password = Md5Encrypt32("ifnk"), MaterialId = 1},
				new User {Id = 15, Name = "xx6", Password = Md5Encrypt32("ifnk"), MaterialId = 1},
				new User {Id = 16, Name = "xx7", Password = Md5Encrypt32("ifnk"), MaterialId = 1},
				new User {Id = 17, Name = "xx8", Password = Md5Encrypt32("ifnk"), MaterialId = 1},
				new User {Id = 18, Name = "xx9", Password = Md5Encrypt32("ifnk"), MaterialId = 1},
				new User {Id = 19, Name = "xx10", Password = Md5Encrypt32("ifnk"), MaterialId = 1},
				new User {Id = 20, Name = "xx11", Password = Md5Encrypt32("ifnk"), MaterialId = 1},
				new User {Id = 21, Name = "xx12", Password = Md5Encrypt32("ifnk"), MaterialId = 1},
				new User {Id = 22, Name = "xx13", Password = Md5Encrypt32("ifnk"), MaterialId = 1},
				new User {Id = 23, Name = "xx14", Password = Md5Encrypt32("ifnk"), MaterialId = 1},
				new User {Id = 24, Name = "xx15", Password = Md5Encrypt32("ifnk"), MaterialId = 1},
				new User {Id = 25, Name = "xx16", Password = Md5Encrypt32("ifnk"), MaterialId = 1},
				new User {Id = 26, Name = "xx17", Password = Md5Encrypt32("ifnk"), MaterialId = 1},
				new User {Id = 27, Name = "xx18", Password = Md5Encrypt32("ifnk"), MaterialId = 1},
				new User {Id = 28, Name = "xx19", Password = Md5Encrypt32("ifnk"), MaterialId = 1},
				new User {Id = 29, Name = "xx20", Password = Md5Encrypt32("ifnk"), MaterialId = 1},
				new User {Id = 30, Name = "xx21", Password = Md5Encrypt32("ifnk"), MaterialId = 1},
				new User {Id = 31, Name = "xx22", Password = Md5Encrypt32("ifnk"), MaterialId = 1},
				new User {Id = 32, Name = "xx23", Password = Md5Encrypt32("ifnk"), MaterialId = 1},
				new User {Id = 33, Name = "xx24", Password = Md5Encrypt32("ifnk"), MaterialId = 1},
				new User {Id = 34, Name = "xx25", Password = Md5Encrypt32("ifnk"), MaterialId = 1},
				new User {Id = 35, Name = "xx26", Password = Md5Encrypt32("ifnk"), MaterialId = 1},
				new User {Id = 36, Name = "xx27", Password = Md5Encrypt32("ifnk"), MaterialId = 1},
				new User {Id = 37, Name = "xx28", Password = Md5Encrypt32("ifnk"), MaterialId = 1},
				new User {Id = 38, Name = "xx29", Password = Md5Encrypt32("ifnk"), MaterialId = 1},
				new User {Id = 39, Name = "dudu", Password = Md5Encrypt32("ifnk"), MaterialId = 1},
				new User {Id = 999, Name = "开心超人", Password = Md5Encrypt32("ifnk"), MaterialId = 1}
			);

			modelBuilder.Entity<Role>().HasData(
				new Role {Id = 1, Name = "admin", Desc = "普通管理员"},
				new Role {Id = 2, Name = "system", Desc = "系统管理员"},
				new Role {Id = 3, Name = "Tibug", Desc = "tibug系统管理"},
				new Role {Id = 4, Name = "SuperAdmin", Desc = "超级管理"},
				new Role {Id = 5, Name = "AdminTest", Desc = "管理测试"},
				new Role {Id = 6, Name = "AdminTest2", Desc = "管理测试2"},
				new Role {Id = 7, Name = "test", Desc = "dudu测试"},
				new Role {Id = 25, Name = "sss", Desc = "sss"},
				new Role {Id = 26, Name = "213", Desc = "213"},
				new Role {Id = 27, Name = "213", Desc = "213"},
				new Role {Id = 28, Name = "hello1", Desc = "后台总管理员"},
				new Role {Id = 999, Name = "system", Desc = "系统管理员"}
			);


			modelBuilder.Entity<Material>()
				.HasData(new Material()
				{
					Id = 1, Name = "小埋", RealName = "小埋-1606637124806",
					Suffix = "jpg",
					Path = "J:\\Inspection\\InspectionApi\\wwwroot\\file\\小埋-1605938092211.jpg",
					Type = FileType.图片,
					Size = 36793
				});
			modelBuilder.Entity<Menu>().HasData(
				new Menu() {Id = 18, Name = "QQ欢迎页", PId = 0, Path = "/", Icon = "fa-qq"},
				// 用户角色管理
				new Menu() {Id = 19, Name = "用户角色管理", PId = 0, Path = "-", Icon = "fa-users"},
				new Menu() {Id = 20, Name = "角色管理", PId = 19, Path = "/User/Roles", Icon = "fa-users"},
				new Menu() {Id = 21, Name = "用户管理", PId = 19, Path = "/User/users", Icon = "fa-users"},
				// 菜单权限管理
				new Menu() {Id = 24, Name = "菜单管理", PId = 19, Path = "/Permission/Permission", Icon = "fa-sitemap"}
			);
			modelBuilder.Entity<UserRole>().HasData(
				new UserRole {UserId = 4, RoleId = 1},
				new UserRole {UserId = 3, RoleId = 2},
				new UserRole {UserId = 7, RoleId = 3},
				new UserRole {UserId = 12, RoleId = 4},
				new UserRole {UserId = 1, RoleId = 2},
				new UserRole {UserId = 1, RoleId = 1},
				new UserRole {UserId = 2, RoleId = 1},
				new UserRole {UserId = 8, RoleId = 6},
				new UserRole {UserId = 13, RoleId = 7},
				new UserRole {UserId = 39, RoleId = 28}
			);
			

			modelBuilder.Entity<RoleMenu>().HasData(
				// 管理员 角色  什么菜单 都有
				new RoleMenu {Id = 4, RoleId = 1, MenuId = 18},
				new RoleMenu {Id = 4, RoleId = 1, MenuId = 19},
				new RoleMenu {Id = 4, RoleId = 1, MenuId = 20},
				new RoleMenu {Id = 4, RoleId = 1, MenuId = 21},
				new RoleMenu {Id = 4, RoleId = 1, MenuId = 24},
				// 测试 角色  只有一个欢迎页
				new RoleMenu {Id = 4, RoleId = 7, MenuId = 18}
			);
		}
	}
}